## Chapter 1
## Graph of over-time Affective Polarization from ANES 
## Adapted from OCB for Druckman et al. Project 

## Read in the data 
cdf <- read_dta(file="~/Dropbox/Textbook/Big Book/17th enhanced edition/chapters/Figures/NES Data/anes_timeseries_cdf_stata_20211118.dta") 

## In the cumulative data, get the estimate of in-party and out-party ratings 
## for 2012 forward, do this separately by mode (FTF vs. Internet)
cdf <- cdf %>% 
  mutate(dem_ft = ifelse(VCF0218 < 98,VCF0218,NA),
         rep_ft = ifelse(VCF0224 < 98,VCF0224,NA),
         dem_cand_ft = ifelse(VCF0424 < 98, VCF0424, NA), 
         rep_cand_ft = ifelse(VCF0426 < 98, VCF0426, NA),
         pid = ifelse(VCF0301 > 0 & VCF0301 < 8, VCF0301, NA),
         dem = ifelse(pid<4,1,
                      ifelse(pid > 4,0,NA)), 
         same_ft = ifelse(pid < 4,dem_ft,
                          ifelse(pid > 4,rep_ft,NA)),
         other_ft = ifelse(pid > 4,dem_ft,
                           ifelse(pid < 4,rep_ft,NA)),
         ft_diff = same_ft - other_ft, 
         same_cand_ft = ifelse(pid < 4,dem_cand_ft,
                               ifelse(pid > 4,rep_cand_ft,NA)),
         other_cand_ft = ifelse(pid > 4,dem_cand_ft,
                                ifelse(pid < 4,rep_cand_ft,NA)),
         year = VCF0004, 
         ftf_interview = ifelse(VCF0017 == 0,1,
                                ifelse(VCF0017 == 4,0,NA))) 

## average FT rating (same party and other-party)
avg_fts <- cdf %>% 
  filter((year > 1977) & year != 2002 & !is.na(ftf_interview)) %>% 
  group_by(year,ftf_interview) %>% 
  summarise(avg_same = mean(same_ft, na.rm=T),
            avg_other = mean(other_ft, na.rm=T))

## group FT rating into one variable 
avg_fts <- avg_fts %>% 
  gather(key = label, 
         value = FT_Measure,
         c(avg_same,avg_other))

## for plotting, need a code that tells you same vs. other party & mode (ftf vs. internet)
avg_fts <- avg_fts %>% 
  mutate(target_mode = case_when(
    ftf_interview == 1 & label == "avg_same" ~ 1, 
    ftf_interview == 1 & label == "avg_other" ~ 2, 
    ftf_interview == 0 & label == "avg_same" ~ 3, 
    ftf_interview == 0 & label == "avg_other" ~ 4))  

## plot! 
pdf(file = "Figures/Druckman_etal_figure_1_1.pdf",
    height = 11,
    width = 8.5)
print(avg_fts %>% 
  ggplot(aes(year,FT_Measure, 
             group = target_mode)) + 
  geom_line(aes(color = as.factor(target_mode),
                lty = as.factor(target_mode))) + 
  scale_color_manual(name = "",
                     values = c("black","darkgray","black","darkgray"), 
                     labels = c("In-Party (FTF)","Out-Party (FTF)",
                                "In-Party (Online)","Out-Party (Online)")) + 
  scale_linetype_manual(name = "",
                        values = c("solid","solid","dotted","dotted"), 
                        labels = c("In-Party (FTF)","Out-Party (FTF)",
                                   "In-Party (Online)","Out-Party (Online)")) + 
  scale_x_continuous(breaks = c(1980,1988,1996,2004,2012,2020)) + 
  ylim(c(0,100)) + 
  xlab("Year") + 
  ylab("Feeling Thermometer Rating") + 
  #ggtitle("Partisan Sentiment Over Time") +  
  theme_bw() + 
  #guides(color=guide_legend(nrow=2,byrow=TRUE))+
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank())) 
dev.off() 

